package com.common;

import com.config.exception.GlobalException;
import lombok.Data;

@Data
public class UIResult {

    // 返回状态
    private String status;
    //返回编码
    private String code;
    //编码描述
    private String msg;
    //业务数据
    private Object data;

    private UIResult(String status, String code, String msg) {
        this.status = status;
        this.code = code;
        this.msg = msg;
    }

    public static UIResult Error(ErrorCode errorCode) {
        return new UIResult(Const.ERROR_STATUS, errorCode.getCode(), errorCode.getMsg());
    }

    public static UIResult Error(GlobalException gex) {
        return new UIResult(Const.ERROR_STATUS, gex.getMessage(), gex.getCode());
    }

    public static UIResult Success(ErrorCode errorCode) {
        return new UIResult(Const.SUCCESS_STATUS, errorCode.getCode(), errorCode.getMsg());
    }

    public UIResult setMsg(String msg) {
        if (StringUtil.isEmpty(this.msg)) {
            ErrorCode serverError = ErrorCode.SERVER_ERROR;
            this.msg = serverError.getMsg();
            this.code = serverError.getCode();
        } else {
            this.msg = msg;
        }
        return this;
    }

}
